import BaseSprite from "../../../Script/Base/BaseSprite";
import EventID from "../../../Script/Common/EventID";
import { GlobalData } from "../../../Script/Common/GlobalData";
import { TalkModule } from "../../../Script/Common/TalkModule";
import { IGameData, IMessage, ISelectData, ITalkData } from "../../../Script/Interface/IMessage";
import { SceneGame } from "../../../Script/SceneGame";
import GameUtil from "../../../Script/Util/GameUtil";
import StorageUtil from "../../../Script/Util/StorageUtil";
import Message from "../../Common/Message";

const { ccclass, property } = cc._decorator;

export enum EPlayType {
    null = -1,
    Mine,           // 矿山
    Market,         // 地摊
    Hospital,       // 御康馆
    Store,          // 小卖店
    Lottery,        // 刮刮乐
    LinYao,         // 林瑶
}

@ccclass
export default class TownUI extends BaseSprite implements IMessage {
    public static instance: TownUI = null;

    @property({ type: [cc.Node], tooltip: "背景白天/晚上" })
    private bg: cc.Node[] = [];

    @property({ type: cc.Node, tooltip: "疯老头对话层" })
    private talk: cc.Node = null;
    @property({ type: sp.Skeleton, tooltip: "疯老头对话层--spine" })
    private ske: sp.Skeleton = null;

    @property({ type: [cc.Node], tooltip: "小玩法按钮" })
    private btns: cc.Node[] = [];

    @property({ type: cc.Node, tooltip: "林瑶" })
    private linYao: cc.Node = null;
    @property({ type: cc.Node, tooltip: "疯老头" })
    private laoTou: cc.Node = null;

    @property({ type: cc.Node, tooltip: "rect" })
    private testRect: cc.Node = null;

    private gameData: IGameData = null;
    private talkData: ITalkData = null;

    recvMsg(cmd: number, data: any) {
        switch (cmd) {
            case EventID.TEST_CMD:
                {

                }
                break;
        }
    }

    protected onLoad() {
        this.addMessage(EventID.TEST_CMD, this);

        TownUI.instance = this;

        this.gameData = StorageUtil.instance.getGameData();
        this.talkData = StorageUtil.instance.getTalkData();
    }

    protected onEnable(): void {
        this.check();
    }

    public check() {
        // 昼夜切换
        TownUI.instance.changeDayorEvening();
        // 乞丐弹窗
        this.showPauper();
        // 展示矿村箭头提示
        this.showArrow();
        // 展示林瑶
        this.showLinYao();
        // 展示疯老头
        this.showLaoTou();
        // 继续流程
        this.updateLine();
        this.showTalk();
    }

    /** 和疯老头对话 */
    private showTalk() {
        this.talk.active = this.talkData.id >= 10401 && this.talkData.id < 10411;

        const config = TalkModule.getSayConfigById(this.talkData.id);
        const bone = this.ske.findBone('张嘴');
        if (bone) {
            bone.active = config && config.name === '老乞丐';
        }

        if (this.talk.active) {
            SceneGame.instance.hideTopUI();
        } else {
            SceneGame.instance.showTopUI();
        }
    }

    /** 更新玩法入口箭头 */
    private updateArrow(type: EPlayType) {
        this.btns.forEach((o, i) => {
            const arrow = cc.find('箭头', o);
            arrow.active = i === type;
        });
    }

    private updateLine() {
        switch (this.talkData.id) {
            case 10552:
                this.talkData.id = 10601;
                TalkModule.updateCheck();
                break;
            case 10806:
                // 医馆学徒
                const hospitalData = StorageUtil.instance.getHospitalData();
                if (hospitalData.id >= 3) {
                    this.talkData.id = 10900;
                    TalkModule.updateCheck();
                }
                break;
            case 11106:
                SceneGame.instance.showPopView('PactUI', { a: "花娟娟", b: "顾云泽", money: 5000000 });
                break;
        }
    }

    private showArrow() {
        if (this.talkData.id === 10204 || this.talkData.id === 10510 || this.talkData.id === 11001) {
            const isComplete = GlobalData.checkCollectComplete();
            if (isComplete) {
                this.updateArrow(EPlayType.LinYao);
            } else {
                this.updateArrow(EPlayType.Mine);
            }
        } else if (this.talkData.id === 10305) {
            this.updateArrow(EPlayType.Market);
        } else if (this.talkData.id === 10608) {
            this.updateArrow(EPlayType.Hospital);
        } else if (this.talkData.id === 11008 || this.talkData.id === 11104 || this.talkData.id === 11105) {
            this.updateArrow(EPlayType.Store);
        } else {
            this.updateArrow(-1);
        }
    }

    /** 弹出乞丐弹窗 */
    public showPauper() {
        if (this.talkData.id !== 10307) {
            return;
        }

        if (!this.node.active) {
            return;
        }

        const callback = () => {
            this.talkData.id = 10401;
            // TalkModule.updateCheck();
        }
        const data: ISelectData = {
            desc: "你刚走出集市门口，一名蓬头垢面的乞丐老人拦住了你的去路",
            success: { lab: "听他说话", callback },
            fail: {
                lab: "强制离开", next: {
                    desc: "你被一个扫堂腿撂倒，腿部骨折，是否选择治愈",
                    success: {
                        lab: "免费治愈", isVideo: true, callback
                    },
                    fail: {
                        lab: "诊所治疗", value: 6000, callback
                    },
                },
            },
            callback: ()=>{
                Message.instance.send(EventID.HIDE_VIEW_CLOSE, { hide: 'back' });
            }
        }
        SceneGame.instance.showPopView("SelectUI", data);
    }

    public showLinYao() {
        this.linYao.active = false;
        if (this.talkData.id === 10510 || this.talkData.id === 10551 || this.talkData.id === 11001) {
            this.linYao.active = true;
        }
    }

    public showLaoTou() {
        this.laoTou.active = this.talkData.id >= TalkModule.OPEN_NITPICK;
        // 林瑶出现的时候老头不出现
        if (this.laoTou.active) {
            this.laoTou.active = !this.linYao.active;
        }
    }

    public changeDayorEvening() {
        const timeData = StorageUtil.instance.getTimeData();
        if (timeData) {
            const isDay = timeData.hour >= 3 && timeData.hour < 20;
            if (this.bg[0]) {
                this.bg[0].active = isDay;
            }
            if (this.bg[1]) {
                this.bg[1].active = !isDay;
            } 
        }
    }

    private onBtnsClicked(event: cc.Event.EventTouch, eventData: string) {
        const bHit = GameUtil.checkClicked(event.currentTarget, this.testRect, event);
        if (bHit) {
            const name = event.target.name;
            switch (name) {
                case 'btnMine':
                    {
                        if (this.talkData.id < TalkModule.OPEN_MINE) {
                            SceneGame.instance.showToast(cc.v2(0, 0), '暂未开放~');
                            return;
                        }
                        SceneGame.instance.showPopView('MineUI');
                    }
                    break;
                case 'btnMarket':
                    {
                        if (this.talkData.id < 35) {
                            SceneGame.instance.showToast(cc.v2(0, 0), '暂未开放~');
                            return;
                        }
                        SceneGame.instance.showPopView('MarketUI');
                    }
                    break;
                case 'btnHospital':
                    {
                        if (this.talkData.id < TalkModule.OPEN_HOSPITAL) {
                            SceneGame.instance.showToast(cc.v2(0, 0), '暂未开放~');
                            return;
                        }

                        if (this.talkData.id >= 10802) {
                            const hospitalData = StorageUtil.instance.getHospitalData();
                            if (hospitalData.student === 2 && this.gameData.brain < 55) {
                                SceneGame.instance.showToast(cc.v2(0, 0), '智商未达到55, 被刘神医拒之门外');
                                // // 功法小手提示
                                // GameUI.instance.showNitpickTip();
                                return;
                            }
                        }

                        SceneGame.instance.showPopView('HospitalUI');
                    }
                    break;
                case 'btnStore':
                    {
                        if (this.talkData.id < TalkModule.OPEN_STORE) {
                            SceneGame.instance.showToast(cc.v2(0, 0), '暂未开放~');
                            return;
                        }

                        // 签约订单
                        if (this.talkData.id === 11106) {
                            SceneGame.instance.showPopView('PactUI', { a: "花娟娟", b: "顾云泽", money: 5000000 });
                            return;
                        }

                        SceneGame.instance.showPopView('StoreUI');
                    }
                    break;
                case 'btnLottery':
                    {
                        SceneGame.instance.showPopView('LotteryUI');
                    }
                    break;
                case 'btnLinYao':
                    {
                        const data = GlobalData.getCollectData();
                        if (!data) {
                            SceneGame.instance.showToast(cc.v2(0, 0), '暂时没有收集任务~');
                            return;
                        }

                        SceneGame.instance.showPopView('CollectUI');
                    }
                    break;
                case 'btnLaoTou':
                    console.log("疯老头~");
                    break;
            }
        }
    }
}